﻿using System;
using System.Collections.Generic;
using System.Text;
using Cinema.GUI;
using System.Windows.Forms;
using System.Threading;
using Cinema.c_dbDataSetTableAdapters;
using System.Data;

namespace Cinema.Control
{
    public abstract class MainClass
    {
        //vars
            //--
        static CustomerTableAdapter customerTableAdapter = new CustomerTableAdapter();
        static EmployeeTableAdapter employeeTableAdapter = new EmployeeTableAdapter();
        static MemberTableAdapter memberTableAdapter = new MemberTableAdapter();
        static MovieTableAdapter movieTableAdapter = new MovieTableAdapter();
        static ProjectionTableAdapter projectionTableAdapter = new ProjectionTableAdapter();
        static SaloonTableAdapter saloonTableAdapter = new SaloonTableAdapter();
        static TicketTableAdapter ticketTableAdapter = new TicketTableAdapter();
            //--
        public static c_dbDataSet.CustomerDataTable dataTableCustomer = customerTableAdapter.GetData();
        public static c_dbDataSet.EmployeeDataTable dataTableEmployee = employeeTableAdapter.GetData();
        public static c_dbDataSet.MemberDataTable dataTableMember = memberTableAdapter.GetData();
        public static c_dbDataSet.MovieDataTable dataTableMovie = movieTableAdapter.GetData();
        public static c_dbDataSet.ProjectionDataTable dataTableProjection = projectionTableAdapter.GetData();
        public static c_dbDataSet.SaloonDataTable dataTableSaloon = saloonTableAdapter.GetData();
        public static c_dbDataSet.TicketDataTable dataTableTicket = ticketTableAdapter.GetData();
            //--
        public static Boolean logged = false;
        public static String username;
        public static Boolean updating = true;
        public static MainFrame mf;
        //vars

        //context
        public static void Main(String[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            updating = true;
            Thread threadShowMain = new Thread(new ThreadStart(MainClass.methodShowMain));
            threadShowMain.Start();
            Thread threadShowLog = new Thread(new ThreadStart(MainClass.methodShowLog));
            threadShowLog.Start();
            Thread threadDataUpdate = new Thread(new ThreadStart(MainClass.dataUpdate));
            threadDataUpdate.Start();
        }

        public static void methodShowMain()
        {
            mf = new MainFrame();
            Application.Run(mf);
        }

        public static void methodShowLog()
        {
            if (!logged)
            {
                Application.Run(new LogPrompt());
            }
        }

        public static void dataUpdate()
        {
            while (true)
            {
                updating = true;
                dataTableCustomer = customerTableAdapter.GetData();
                dataTableEmployee = employeeTableAdapter.GetData();
                dataTableMember = memberTableAdapter.GetData();
                dataTableMovie = movieTableAdapter.GetData();
                dataTableProjection = projectionTableAdapter.GetData();
                dataTableSaloon = saloonTableAdapter.GetData();
                dataTableTicket = ticketTableAdapter.GetData();
                updating = false;
                Thread.Sleep(5000);
            }
        }

        public static Boolean login(String username, String pass)
        {
            while (updating)
            {
                Thread.Sleep(1000);
            }
            logged = false;
            DataRow row = null;
            foreach (DataRow tempRow in dataTableEmployee.Rows)
            {
                DataColumn columnUname = dataTableEmployee.Columns[dataTableEmployee.Columns.IndexOf("uname")];
                if ((tempRow[columnUname].ToString()).Equals(username))
                {
                    row = tempRow;
                    break;
                }
            }
            try
            {
                DataColumn columnPass = dataTableEmployee.Columns[dataTableEmployee.Columns.IndexOf("pass")];
                if (row[columnPass].ToString().Equals(pass))
                {
                    logged = true;
                }
            }
            catch(Exception x)
            {
                Console.WriteLine("Login error! " + x.Message);
            }
            return logged;
        }

        public static int getSaloons()
        {
            return dataTableSaloon.Rows.Count;
        }

        public static String getSaloonData(int index, String req)
        {
            String data = null;
            if(req.Equals("signature"))
            {
                data = dataTableSaloon.Rows[index][dataTableSaloon.Columns[dataTableSaloon.Columns.IndexOf("saloonsignature")]].ToString();
            }
            else if (req.Equals("seats"))
            {
                data = dataTableSaloon.Rows[index][dataTableSaloon.Columns[dataTableSaloon.Columns.IndexOf("seats")]].ToString();
            }
            else if (req.Equals("rows"))
            {
                data = dataTableSaloon.Rows[index][dataTableSaloon.Columns[dataTableSaloon.Columns.IndexOf("rows")]].ToString();
            }
            else
            {
            }
            return data;
        }

        //context
    }
}
